package isc.unicrox.virtualsekai;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class Gamer extends Service{
	
	public int GameModel = 0x0000;
	public static final int GAME_MODEL_ASSEMBLE = 0x0001;
	public boolean GameAssembleRunning = false;
	
	public int GameRole = 0x2000;
	public static final int GAME_ROLE_NORMAL = 0x2001;
	public static final int GAME_ROLE_MANAGER = 0x2003;
	public static final int GAME_ROLE_TARGET = 0x2007;
	
	public int GeotableId = 86772;
	public int PoiId = -1;
	
	public Handler handler;
	public Gamer gamer = this;
	public Context  context = this;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		handler = new Handler();
		Var.GamerHandler = handler;
		Var.GamerSerivce = this;
	}
	
	private void startHttpGet(final String url){
    	new Thread(new Runnable(){
			public void run() {
				// TODO Auto-generated method stub
				HttpGet httpGet = new HttpGet(url);
				HttpResponse httpResponse = null;
		    	try {
					httpResponse = new DefaultHttpClient().execute(httpGet);
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		    	final HttpResponse httpResponseFinal = httpResponse;
		    	if(httpResponse != null){
		    		handler.post(new Runnable() {
						public void run() {
							
						}
					});
		    	}
		    	
			}
    	}).start();
    }
    
    private void startHttpPost(final String url, final ArrayList<NameValuePair> params){
    	new Thread(new Runnable(){
			public void run() {
				// TODO Auto-generated method stub
				HttpPost httpPost = new HttpPost(url);
				HttpResponse httpResponse = null;
		    	try {
		    		httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
					httpResponse = new DefaultHttpClient().execute(httpPost);
					final String result = EntityUtils.toString(httpResponse.getEntity());
					if(httpResponse != null){
			    		handler.post(new Runnable() {
							public void run() {
								gamer.httpback(url, result);
							}
						});
			    	}
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
    	}).start();
    }
    
    private void httpback(String url, String result){
    	Var.DebugString = result;
    }
    
    public void gamePrepare(int model, int role){
    	GameModel = model;
    	GameRole = role;
    	if(GameModel == GAME_MODEL_ASSEMBLE){
    		
    	}
    }
    
    public void gameStart(){
    	if(GameModel == GAME_MODEL_ASSEMBLE){
    		if(PoiId < 0){
    			ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    			params.add(new BasicNameValuePair("geotable_id", String.valueOf(GeotableId)));
    			params.add(new BasicNameValuePair("ak", String.valueOf(Var.ak)));
    			params.add(new BasicNameValuePair("latitude", String.valueOf(Var.LocationLatitude)));
    			params.add(new BasicNameValuePair("longitude", String.valueOf(Var.LocationLontitude)));
    			params.add(new BasicNameValuePair("coord_type", String.valueOf(2)));
    			params.add(new BasicNameValuePair("title", "hello"));
    			startHttpPost(Var.BaiduApi+Var.ApiPoiCreate, params);
    			//Map<String, Object> params = new HashMap<String, Object>();
    			//params.put("geotable_id", GeotableId);
    			//params.put("ak", Var.ak);
    			//params.put("latitude", Var.LocationLatitude);
    			//params.put("longitude", Var.LocationLontitude);
    			//params.put("coord_type", 2);
    			//params.put("title", "hello");
    		}
    	}
    }
    
    public void gamePause(){
    	
    }
    
    public void gameStop(){
    	
    }

}
